﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Gaiocorp.SLMapViewer
{
    public partial class TreeViewNodeLayer : UserControl
    {
        #region Private Properties
        private string _LayerName;
        private int _LayerIndex;
        private bool _IsActive;
        private string _sessionid;
        private Color _backroundColor=Colors.White;
        private Color _titleColor=Colors.Black;
        private double _bakOpacity = 1.0;
        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets layer visibility
        /// </summary>
        public bool IsChecked
        {
            get { return Convert.ToBoolean(chkBoxLayer.IsChecked); }
            set 
            {
                chkBoxLayer.IsChecked = value;
            }
        }
        /// <summary>
        /// Gets or sets Layer Title
        /// </summary>
        public string Title
        {
            get { return tbLayer.Text; }
            set
            {
                tbLayer.Text = value;
            }
        }
        /// <summary>
        /// The layer name as defined in the mapfile
        /// </summary>
        public string LayerName
        {
            get { return _LayerName; }
            set { _LayerName = value; }
        }
        /// <summary>
        /// Gets the layer index as defined in the mapfile
        /// </summary>
        public int LayerIndex
        {
            get { return _LayerIndex; }
        }
        /// <summary>
        /// Gets or sets or sets active legend layer
        /// </summary>
        public bool IsActive
        {
            get { return _IsActive; }
            set 
            { 
                _IsActive = value;
                this.SetLayerColor();
            }
        }
        public Color BackroundColor
        {
            get { return _backroundColor; }
            set
            {
                _backroundColor = value;
                this.LayoutRoot.Background = new SolidColorBrush(_backroundColor);
            }
        }
        public Color TitleColor
        {
            get { return _titleColor; }
            set
            {
                _titleColor = value;
                this.tbLayer.Foreground = new SolidColorBrush(_titleColor);
            }
        }
        public double BackgroundOpacity
        {
            get { return _bakOpacity; }
            set
            {
                _bakOpacity = value;
                this.LayoutRoot.Opacity = _bakOpacity;
            }
        }
        #endregion

        public delegate void LegendControl_Clicked(object sender,string xmlRequest);
        public event LegendControl_Clicked Clicked;
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="layerIndex">new layer index</param>
        public TreeViewNodeLayer(int layerIndex, string sessionid)
        {
            InitializeComponent();
            _LayerIndex = layerIndex;
            _sessionid = sessionid;
        }

        /// <summary>
        /// Checkbox on click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkBoxLayer_Click(object sender, RoutedEventArgs e)
        {
            if (sender.GetType() == typeof(CheckBox))
            {
                Clicked(this, MakeXmlRequest(true));
            }
        }
        /// <summary>
        /// Constructs the Xml Request for the mapserver
        /// </summary>
        /// <param name="RequestLayer">When true sets layer visibility, when false sets active layer</param>
        /// <returns></returns>
        private string MakeXmlRequest(bool RequestLayer)
        {
            string xmlString = string.Empty;
            if (RequestLayer)
            {
                xmlString = @"<REQUEST action='SetLegend' sessionid='" + this._sessionid +"'>" +
                            " <LAYER name='" + this._LayerName + "' visible='" + Convert.ToString(Convert.ToInt32(this.IsChecked)) + "' />"
                        + "</REQUEST>";
            }
            else
            {
                xmlString = @"<REQUEST action='SetLegend' sessionid='" + this._sessionid +"'>" +
                           " <ACTIVE_INDEX>" + this._LayerIndex.ToString()  + "</ACTIVE_INDEX></REQUEST>";
                       
            }

            return xmlString;
        }

        private void tbLayer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (sender.GetType() == typeof(TextBlock))
            {
               //inside a try/catch in case there is no event handler
                try {Clicked(this, MakeXmlRequest(false));}
                catch {}
               
            }
        }
        /// <summary>
        /// Sets the layer label color
        /// </summary>
        private void SetLayerColor()
        {
            if (_IsActive ) 
            { this.tbLayer.Foreground = new SolidColorBrush(Colors.Red); }
            else{ this.tbLayer.Foreground = new SolidColorBrush(this.TitleColor); }
        }
    }
}
